package com.alibaba.system.controller;


import com.alibaba.basic.uitl.AjaxResult;
import com.alibaba.basic.uitl.PageList;
import com.alibaba.system.domain.Detail;
import com.alibaba.system.query.DetailQuery;
import com.alibaba.system.service.DetailService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DetailController {

    @Autowired
    private DetailService detailService;

    @GetMapping
    public List<Detail> findAll(){
        return detailService.findAll();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Detail findById(@ApiParam(value="类型ID",required = true)  @PathVariable("id") Long id) {
        return detailService.finById(id);
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Detail detail){
        if (null == detail.getId()){
            detailService.add(detail);
        } else {
            detailService.update(detail);
        }
        return AjaxResult.me().success();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        detailService.delete(id);
        return AjaxResult.me().success();
    }
    /**
     * 分页查询
     */
    @PostMapping
    public PageList<Detail> queryByPage(@RequestBody DetailQuery query) {
        return detailService.queryByPage(query);
    }





    /**
     *  批量删除
     */
    @PatchMapping
    public AjaxResult deleteId(@RequestBody List<Long> ids) {
        detailService.deleteId(ids);
        return AjaxResult.me().success();
    }

    /**
     * 通过typeID查找
     */
    @GetMapping("/findByTypeId/{id}")
    public List<Detail> findByTypeId(@PathVariable("id") Long id ){
        return detailService.findByTypeId(id);
    }
}
